/* $Id: ViewUnitTest.java 63 2010-02-14 13:52:34Z aaron.day $ */
package org.crazydays.gameplan.view;


import java.beans.PropertyVetoException;

import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.event.InternalFrameEvent;

import org.junit.*;
import static org.junit.Assert.*;

import static org.easymock.EasyMock.*;
import static org.easymock.classextension.EasyMock.*;

import org.crazydays.gameplan.controller.ActionController;
import org.crazydays.gameplan.db.Database;
import org.crazydays.gameplan.map.GameMap;
import org.crazydays.gameplan.map.action.ToggleGrid;
import org.crazydays.gameplan.map.swing.JMapFrame;
import org.crazydays.gameplan.map.swing.MapFrameFactory;
import org.crazydays.gameplan.view.View;

/**
 * ViewUnitTest
 */
public class ViewUnitTest
{
    /**
     * Test initializeMenuBar().
     */
    @Test
    public void testInitializeMenuBar()
    {
        // objects
        FileMenu fileMenu = new FileMenu();
        EditMenu editMenu = new EditMenu();
        ViewMenu viewMenu = new ViewMenu();
        HelpMenu helpMenu = new HelpMenu();

        // setup
        View view = new View();
        view.setFileMenu(fileMenu);
        view.setEditMenu(editMenu);
        view.setViewMenu(viewMenu);
        view.setHelpMenu(helpMenu);

        // test
        view.initializeMenuBar();

        // verify
        assertNotNull("menubar", view.getJMenuBar());
        assertEquals("count", 4, view.getJMenuBar().getComponentCount());
        assertEquals("fileMenu", fileMenu, view.getJMenuBar().getComponent(0));
        assertEquals("editMenu", editMenu, view.getJMenuBar().getComponent(1));
        assertEquals("viewMenu", viewMenu, view.getJMenuBar().getComponent(2));
        assertEquals("helpMenu", helpMenu, view.getJMenuBar().getComponent(3));
    }

    /**
     * Test open(IGameMap).
     * 
     * @throws PropertyVetoException
     */
    @Test
    public void testOpen()
        throws PropertyVetoException
    {
        // objects
        String key = "key";

        // mocks
        GameMap gameMap = createMock(GameMap.class);
        MapFrameFactory mapFrameFactory = createMock(MapFrameFactory.class);
        JMapFrame frame = createMock(JMapFrame.class);
        JDesktopPane desktopPane = createMock(JDesktopPane.class);

        // setup
        View view = new View();
        view.setMapFrameFactory(mapFrameFactory);
        view.desktopPane = desktopPane;

        // expect
        expect(gameMap.getName()).andReturn(key);
        expect(mapFrameFactory.buildMapFrame(gameMap)).andReturn(frame);
        expect(desktopPane.add(frame)).andReturn(frame);
        desktopPane.moveToFront(frame);
        frame.addInternalFrameListener(view);
        frame.setSelected(true);

        // replay
        replay(gameMap, mapFrameFactory, frame, desktopPane);

        // test
        view.open(gameMap);

        // verify
        verify(gameMap, mapFrameFactory, frame, desktopPane);

        assertEquals("openGameMaps", frame, view.openGameMaps.get(key));
    }

    /**
     * Test open(IGameMap).
     */
    @Test(expected = IllegalArgumentException.class)
    public void testOpen_null()
    {
        // setup
        View view = new View();

        // test
        view.open(null);
    }

    /**
     * Test opened(Database).
     */
    @Test
    public void testOpened()
    {
        // setup
        View view = new View();

        // test
        view.opened(null);
    }

    /**
     * Test closed(Database).
     */
    @Test
    public void testClosed()
    {
        // objects
        Database database = new Database();

        // mocks
        JDesktopPane desktopPane = createMock(JDesktopPane.class);
        JMapFrame mapFrame = createMock(JMapFrame.class);

        // setup
        View view = new View();
        view.openGameMaps.put("key", mapFrame);
        view.desktopPane = desktopPane;

        // expect
        expect(desktopPane.getAllFrames()).andReturn(
            new JInternalFrame[] { mapFrame });
        mapFrame.dispose();

        // replay
        replay(desktopPane, mapFrame);

        // test
        view.closed(database);

        // verify
        verify(desktopPane, mapFrame);
    }

    /**
     * Test internalFrameOpened(InternalFrameEvent).
     */
    @Test
    public void testInternalFrameOpened()
    {

        // setup
        View view = new View();

        // test
        view.internalFrameOpened(null);
    }

    /**
     * Test internalFrameActivated(InternalFrameEvent).
     */
    @Test
    public void testInternalFrameActivated_grid_on()
    {
        // mocks
        InternalFrameEvent event = createMock(InternalFrameEvent.class);
        ActionController actionController = createMock(ActionController.class);
        ToggleGrid toggleGrid = createMock(ToggleGrid.class);
        JMapFrame frame = createMock(JMapFrame.class);

        // setup
        View view = new View();
        view.setActionController(actionController);

        // expect
        expect(event.getInternalFrame()).andReturn(frame);
        actionController.enableMapOptions();
        expect(frame.isGridVisible()).andReturn(true);
        expect(actionController.getToggleGrid()).andReturn(toggleGrid);
        toggleGrid.setIconCross();

        // replay
        replay(event, actionController, toggleGrid, frame);

        // test
        view.internalFrameActivated(event);

        // verify
        verify(event, actionController, toggleGrid, frame);
    }

    /**
     * Test internalFrameActivated(InternalFrameEvent).
     */
    @Test
    public void testInternalFrameActivated_grid_off()
    {
        // mocks
        InternalFrameEvent event = createMock(InternalFrameEvent.class);
        ActionController actionController = createMock(ActionController.class);
        ToggleGrid toggleGrid = createMock(ToggleGrid.class);
        JMapFrame frame = createMock(JMapFrame.class);

        // setup
        View view = new View();
        view.setActionController(actionController);

        // expect
        expect(event.getInternalFrame()).andReturn(frame);
        actionController.enableMapOptions();
        expect(frame.isGridVisible()).andReturn(false);
        expect(actionController.getToggleGrid()).andReturn(toggleGrid);
        toggleGrid.setIconTick();

        // replay
        replay(event, actionController, toggleGrid, frame);

        // test
        view.internalFrameActivated(event);

        // verify
        verify(event, actionController, toggleGrid, frame);
    }

    /**
     * Test internalFrameIconified(InternalFrameEvent).
     */
    @Test
    public void testInternalFrameIconified()
    {
        // setup
        View view = new View();

        // test
        view.internalFrameIconified(null);
    }

    /**
     * Test internalFrameDeiconified(InternalFrameEvent).
     */
    @Test
    public void testInternalFrameDeiconified()
    {
        // setup
        View view = new View();

        // test
        view.internalFrameDeiconified(null);
    }

    /**
     * Test internalFrameDeactivated(InternalFrameEvent).
     */
    @Test
    public void testInternalFrameDeactivated()
    {
        // mocks
        InternalFrameEvent event = createMock(InternalFrameEvent.class);
        JMapFrame frame = createMock(JMapFrame.class);
        ActionController actionController = createMock(ActionController.class);

        // expect
        expect(event.getInternalFrame()).andReturn(frame);
        actionController.disableMapOptions();

        // replay
        replay(event, frame, actionController);

        // setup
        View view = new View();
        view.setActionController(actionController);

        // test
        view.internalFrameDeactivated(event);

        // verify
        verify(event, frame, actionController);
    }

    /**
     * Test internalFrameClosing(InternalFrameEvent).
     */
    @Test
    public void testInternalFrameClosing()
    {
        // setup
        View view = new View();

        // test
        view.internalFrameClosing(null);
    }

    /**
     * Test internalFrameClosed(InternalFrameEvent).
     */
    @Test
    public void testInternalFrameClosed()
    {
        // objects
        String name = "name";

        // mocks
        InternalFrameEvent event = createMock(InternalFrameEvent.class);
        JMapFrame frame = createMock(JMapFrame.class);
        GameMap gameMap = createMock(GameMap.class);

        // setup
        View view = new View();
        view.openGameMaps.put(name, frame);

        // expect
        expect(event.getInternalFrame()).andReturn(frame);
        expect(frame.getGameMap()).andReturn(gameMap).anyTimes();
        expect(gameMap.getName()).andReturn(name).anyTimes();
        frame.removeInternalFrameListener(view);

        // replay
        replay(event, frame, gameMap);

        // test
        view.internalFrameClosed(event);

        // verify
        verify(event, frame, gameMap);
        assertTrue("openGameMap", view.openGameMaps.isEmpty());
    }

    /**
     * Test toggleGrid().
     */
    @Test
    public void testToggleGrid_not_map()
    {
        // mocks
        JDesktopPane desktopPane = createMock(JDesktopPane.class);
        JInternalFrame frame = createMock(JInternalFrame.class);

        // setup
        View view = new View();
        view.desktopPane = desktopPane;

        // expect
        expect(desktopPane.getSelectedFrame()).andReturn(frame);

        // replay
        replay(desktopPane, frame);

        // test
        boolean result = view.toggleGrid();

        // verify
        verify(desktopPane, frame);

        assertEquals("result", false, result);
    }

    /**
     * Test toggleGrid().
     */
    @Test
    public void testToggleGrid_make_visible()
    {
        // mocks
        JDesktopPane desktopPane = createMock(JDesktopPane.class);
        JMapFrame mapFrame = createMock(JMapFrame.class);

        // setup
        View view = new View();
        view.desktopPane = desktopPane;

        // expect
        expect(desktopPane.getSelectedFrame()).andReturn(mapFrame);
        expect(mapFrame.isGridVisible()).andReturn(false);
        mapFrame.setGridVisible(true);
        expect(mapFrame.isGridVisible()).andReturn(true);

        // replay
        replay(desktopPane, mapFrame);

        // test
        boolean result = view.toggleGrid();

        // verify
        verify(desktopPane, mapFrame);

        assertEquals("result", true, result);
    }

    /**
     * Test toggleGrid().
     */
    @Test
    public void testToggleGrid_make_invisible()
    {
        // mocks
        JDesktopPane desktopPane = createMock(JDesktopPane.class);
        JMapFrame mapFrame = createMock(JMapFrame.class);

        // setup
        View view = new View();
        view.desktopPane = desktopPane;

        // expect
        expect(desktopPane.getSelectedFrame()).andReturn(mapFrame);
        expect(mapFrame.isGridVisible()).andReturn(true);
        mapFrame.setGridVisible(false);
        expect(mapFrame.isGridVisible()).andReturn(false);

        // replay
        replay(desktopPane, mapFrame);

        // test
        boolean result = view.toggleGrid();

        // verify
        verify(desktopPane, mapFrame);

        assertEquals("result", false, result);
    }
}
